#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>

struct ListNode {
    int val;
    struct ListNode* next;
};

struct ListNode* removeElements(struct ListNode* head, int val) {
    if (head == NULL)
        return head;
    struct ListNode* cur = head, * pre = NULL;
    while (cur->val == val) {
        head = cur->next;
        free(cur);
        cur = head;
        if (head == NULL)
            return head;
    }
    
    pre = head;
    cur = pre->next;
    while (cur) {
        if (cur->val == val) {
            pre->next = cur->next;
            free(cur);
            cur = pre->next;
        }
        else {
            pre = pre->next;
            cur = pre->next;
        }
    }
    return head;
}

struct ListNode* reverseList(struct ListNode* head) {
    if (head == NULL || head->next == NULL)
        return head;
    struct ListNode* tail = head, * newhead = NULL, * cur = NULL, * pre = NULL;

    while (head) {
        tail = head;
        while (tail->next) {
            pre = tail;
            tail = tail->next;
        }
        if (newhead == NULL) {
            newhead = tail;
        }
        if (pre->next == NULL) {
            break;
        }
        else {
            tail->next = pre;
            pre->next = NULL;
        }
    }

    return newhead;
}

struct ListNode* middleNode(struct ListNode* head) {
    if (head == NULL || head->next == NULL)
        return head;
    struct ListNode* slow = head, * fast = head;
    while (fast && fast->next) {
        slow = slow->next;
        fast = fast->next->next;
    }
    return slow;
}

int kthToLast(struct ListNode* head, int k) {
    if (head->next == NULL)
        return head->val;
    struct ListNode* slow = head, * fast = head;
    for (int i = 0; i < k; i++) {
        fast = fast->next;
    }
    while (fast) {
        slow = slow->next;
        fast = fast->next;
    }
    return slow->val;
}

struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) {
    if (list1 == NULL)
        return list2;
    if (list2 == NULL)
        return list1;
    struct ListNode* cur1 = list1, * cur2 = list2, * newnode = NULL, * tail = NULL;
    while (cur1 != NULL && cur2 != NULL) {
        if (cur1->val < cur2->val) {
            if (newnode == NULL) {
                newnode = list1;
                tail = list1;
            }
            else {
                tail->next = cur1;
                tail = tail->next;
            }
            cur1 = cur1->next;
        }
        else {
            if (newnode == NULL) {
                newnode = list2;
                tail = list2;
            }
            else {
                tail->next = cur2;
                tail = tail->next;
            }
            cur2 = cur2->next;
        }
    }
    if (cur1 != NULL) {
        tail->next = cur1;
    }
    if (cur2 != NULL) {
        tail->next = cur2;
    }
    return newnode;
}

bool chkPalindrome(struct ListNode* A) {
    // write code here
    int len = 0;
    struct ListNode* cur = A;
    while (cur) {
        len++;
        cur = cur->next;
    }
    cur = A;
    int num = len / 2;
    while (num--) {
        cur = cur->next;
    }
    struct ListNode* cur2 = reverseList(cur);
    cur = A;
    num = len / 2;
    while (num--) {
        if (cur->val != cur2->val) {
            return false;
        }
    }
    return true;
}

int main() {
    struct ListNode* n1 = (struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode* n2 = (struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode* n3 = (struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode* n4 = (struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode* n5 = (struct ListNode*)malloc(sizeof(struct ListNode));

    struct ListNode* N1 = (struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode* N2 = (struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode* N3 = (struct ListNode*)malloc(sizeof(struct ListNode));

    n1->val = 1;
    n2->val = 2;
    n3->val = 3;
    n4->val = 2;
    n5->val = 1;

    N1->val = 2;
    N2->val = 4;
    N3->val = 6;

    n1->next = n2;
    n2->next = n3;
    n3->next = n4;
    n4->next = n5;
    n5->next = NULL;

    N1->next = N2;
    N2->next = N3;
    N3->next = NULL;

    struct ListNode* cur = n1;

    while (cur) {
        printf("%d->", cur->val);
        cur = cur->next;
    }
    printf("NULL\n");

    //struct ListNode* head = removeElements(n1, 7);
    //struct ListNode* head = reverseList(n1);
    //struct ListNode* head = middleNode(n1);
    //struct ListNode* head = kthToLast(n1, 3);
    //struct ListNode* head = mergeTwoLists(n1, N1);
    bool ans = chkPalindrome(n1);
    printf("%d\n", ans);

    //while (head) {
    //    printf("%d->", head->val);
    //    head = head->next;
    //}
    //printf("NULL\n");

    return 0;
}
